package com.yonyou.iuap.ipaas.connector.sdwcoatencent.internal.service.core;

import cn.hutool.core.lang.Singleton;
import cn.hutool.core.util.ObjectUtil;
import com.yonyou.iuap.ipaas.connector.sdk.api.model.ResponseAttributes;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.api.model.business.BaseBean;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.api.model.core.response.Response;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.internal.SdwcoatencentConnection;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.internal.service.core.function.BatchHandleFunction;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.internal.service.core.function.HandleFunction;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.internal.utils.CommonUtils;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.internal.utils.LogUtils;
import com.yonyou.yonlinker.ysb.runtime.extension.api.runtime.operation.Result;
import java.util.List;

public class BaseService {
    protected final ResponseHandleService responseHandleService = (ResponseHandleService)Singleton.get(ResponseHandleService.class, new Object[0]);

    public BaseService() {
    }

    public final <T> Result<String, ResponseAttributes> handle(SdwcoatencentConnection connection, String url, T param, String serviceCode, HandleFunction<T> handleFunction) {
        String operationName = CommonUtils.getCallerMethodName();
        LogUtils.start(operationName, param);

        try {
            return handleFunction.apply(connection, url, param, serviceCode);
        } catch (Exception var11) {
            LogUtils.printStacktrace(var11);
            String errorMsg = String.format("%s call exception：%s", operationName, var11.getMessage());
            Response response = new Response("999", errorMsg);
            Result<String, ResponseAttributes> result = this.responseHandleService.buildFail(response);
            LogUtils.end(operationName, result.getOutput());
            return result;
        }
    }

    public final <T extends BaseBean> Result<String, ResponseAttributes> batchHandle(SdwcoatencentConnection connection, String url, List<T> params, String serviceCode, BatchHandleFunction<T> batchHandleFunction) {
        String operationName = CommonUtils.getCallerMethodName();
        LogUtils.start(operationName, params);

        try {
            if (ObjectUtil.isEmpty(params)) {
                Response response = new Response("999", "入参为空");
                return this.responseHandleService.buildFail(response);
            } else {
                return batchHandleFunction.apply(connection, url, params, serviceCode);
            }
        } catch (Exception var11) {
            LogUtils.printStacktrace(var11);
            String errorMsg = String.format("%s call exception：%s", operationName, var11.getMessage());
            Response response = new Response("999", errorMsg);
            Result<String, ResponseAttributes> result = this.responseHandleService.buildFail(response);
            LogUtils.end(operationName, result.getOutput());
            return result;
        }
    }
    
}
